package org.lql.algo.codecrush.hot100.linked;

import org.lql.algo.common.ListNode;

/**
 * @author: liangqinglong
 * @date: 2025-10-11 15:59
 * @description: 24. 两两交换链表中的节点 <a href="https://leetcode.cn/problems/swap-nodes-in-pairs/description/?envType=study-plan-v2&envId=top-100-liked">...</a>
 **/
public class SwapPairs {

	public ListNode swapPairs(ListNode head) {
		if (head == null || head.next == null) {
			return head;
		}
		ListNode dummy = new ListNode(0, head);
		ListNode cur = dummy;
		while (cur.next != null && cur.next.next != null) {
			// 1. 获取两个节点
			ListNode node1 = cur.next;
			ListNode node2 = cur.next.next;
			// 2. 交换节点
			cur.next = node2;
			node1.next = node2.next;
			node2.next = node1;
			cur = cur.next.next;
		}
		return dummy.next;
	}
}
